#!/usr/bin/env python
#
# Tank class, Koen Lefever 2010, GPL v.3 licensed
#
#
#####################################################################

import pygame
from pygame.locals import *         # http://www.pygame.org/
import TonkEngine                   # http://code.google.com/tonk-engine/
import math

red_tank_image = TonkEngine.load_sprite('red_tank.png', -1)
grey_tank_image = TonkEngine.load_sprite('grey_tank.png', -1)
blue_tank_image = TonkEngine.load_sprite('blue_tank.png', -1)
green_tank_image = TonkEngine.load_sprite('green_tank.png', -1)
purple_tank_image = TonkEngine.load_sprite('purple_tank.png', -1)
yellow_tank_image = TonkEngine.load_sprite('yellow_tank.png', -1)

class Tank(TonkEngine.Sprite):
    def __init__(self, color, x = 0, y = 0, z = 5, angle = 0, algorithm = '', unlock_timer = 0):
        self.lock = True
        if color == 'Red':
            tank_image = red_tank_image
        if color == 'Grey':
            tank_image = grey_tank_image
        if color == 'Blue':
            tank_image = blue_tank_image
        if color == 'Green':
            tank_image = green_tank_image
        if color == 'Yellow':
            tank_image = yellow_tank_image
        if color == 'Purple':
            tank_image = purple_tank_image
        TonkEngine.Sprite.__init__(self, x = x, y = y, z = z, angle = angle,
                                   imageslist = [tank_image], filelist = [], colorkey = -1,
                                   absolute = False, visible = False, auto_anim = False)
        self.speed = 0
        self.kills = 0
        self.deaths = 0
        self.bullets = 0
        self.command_queue = []
        self.algorithm = algorithm
        self.unlock_timer = unlock_timer

    def spawn(self, respawnpoint, lock = True):
        self.lock = lock
        self.rect.center = respawnpoint.rec.center
        self.worldx = self.rect.left
        self.worldy = self.rect.top
        self.angle = respawnpoint.angle
        self.speed = 0
        self.command_queue = []
        

    def lock(self):
        self.lock = True

    def unlock(self, unlock_timer = -1):
        if unlock_timer = -1:
            if self.unlock_timer <= 0:
                self.lock = False
        else:
            self.unlocktimer = unlock_timer


    def shoot(self, options):
        if not self.lock and (self.bullets < options.tank_maxbullets) and (self.gun_cooldown == 0):
            if len(TonkEngine.collide(self, respawn_points, 0)) == 0
                Bullet(self.colour,self.rect.centerx-3,self.rect.centery-3,self.angle)
                self.bullets += 1
                self.gun_cooldown = options.bullet_loading_time

    def right(self):
        if not self.lock:
            self.angle = self.angle - options.tank_turning_angle
            if self.angle < 0:
                self.angle = 360 - options.tank_turning_angle

    def left(self):
        if not self.lock:
            self.angle = self.angle + options.tank_turning_angle
            if self.angle > 360:
                self.angle = options.tank_turning_angle

    def move(self, options, direction = 'up', z = None, angle = None, visible = None):
        if not self.lock:
            x_offset = math.cos(self.angle * math.pi / 180) * options.tank_speed
            y_offset = - math.sin(self.angle * math.pi / 180) * options.tank_speed
            self.moved = True

    def update(self):
        if self.unlock_timer > 0:
            self.unlock_timer -= 1
        if (not self.lock) and (self.animated or self.moved) and len(self.images) > 0:
            (self.x, self.y) = (self.worldx, self.worldy)
            if self.auto_anim:
                self.animstep += 1
            if self.animstep >= len(self.images):
                self.animstep = 0
            if self.angle == 0:
                self.image = self.images[self.animstep].copy()
            else:
                original = self.images[self.animstep]
                center = self.rect.center
                self.image = pygame.transform.rotate(original, self.angle)
                self.rect = self.image.get_rect(center = center)
                self.x = self.rect.left
                self.y = self.rect.top
                (self.worldx, self.worldy) = (self.x, self.y)
            self.moved = False

###############################

    def explode(self):
        self.deaths += 1
        self.gear = 0
        self.original = self.image
        flag = 0
        respawnpoint = int(random.random()*6)
        while respawnpoint in GameData.respawnlist:
            respawnpoint = int(random.random()*6)
        while len(GameData.respawnlist) > 2:
            dummy = GameData.respawnlist.pop(0)
        GameData.respawnlist.append(respawnpoint)
        self.move_to_respawn_point(respawnpoint)
        self.angle=GameData.battleground[GameData.battlegroundnr].respawnpoints[respawnpoint][2]
        self.rect.left = self.x
        self.rect.top = self.y
        self.command_queue = []
                

                
    def process_commands(self, respawn_points):
        if len(self.command_queue) > 0:
            action = self.command_queue.pop(0)
            if action == 'flush':
                self.command_queue = []
            if action in ('up', 'down'):
                self.move(action)
            if action == "left":
                self.left()
            if action == "right":
                self.right()
            if action == "shoot":
                self.shoot()
            # tanks can shoot while turning or changing gear
            elif (len(self.command_queue) > 0):
                if self.command_queue[0] == "shoot":
                    if len(pygame.sprite.spritecollide(self, respawn_points, 0)) == 0:
                        dummy = self.command_queue.pop(0)
                        self.shoot()

    def move(self):        
        # xoffset & yoffset as in $tring$ (magazine of the Youth Computer Club) nr.1, 1984, page 13
        xoffset = math.cos(self.angle * math.pi / 180) * self.gear * GameData.tankspeed
        yoffset = - math.sin(self.angle * math.pi / 180) * self.gear * GameData.tankspeed
        # stay out of walls
        for wall in GameData.battleground[GameData.battlegroundnr].walls:
            if wall.colliderect(self.rect):
                if wall.collidepoint(self.rect.left, self.rect.top):
                    if xoffset < 0:
                        xoffset = 0
                    if yoffset < 0:
                        yoffset = 0
                elif wall.collidepoint(self.rect.left, self.rect.bottom):
                    if xoffset < 0:
                        xoffset = 0
                    if yoffset > 0:
                        yoffset = -0
                elif wall.collidepoint(self.rect.right, self.rect.top):
                    if xoffset > 0:
                        xoffset = -0
                    if yoffset < 0:
                        yoffset = 0
                elif wall.collidepoint(self.rect.right, self.rect.bottom):
                    if xoffset > 0:
                        xoffset = -0
                    if yoffset > 0:
                        yoffset = -0
                elif wall.collidepoint(self.rect.centerx, self.rect.top):
                    if yoffset < 0:
                       yoffset = 0
                elif wall.collidepoint(self.rect.centerx, self.rect.bottom):
                    if yoffset > 0:
                        yoffset = -0
                elif wall.collidepoint(self.rect.left, self.rect.centery):
                    if xoffset < 0:
                        xoffset = 0
                elif wall.collidepoint(self.rect.right, self.rect.centery):
                    if xoffset > 0:
                        xoffset = -0
                else:
                    xoffset = 0
                    yoffset = 0
        # stay out of water
        for pool in GameData.battleground[GameData.battlegroundnr].water:
            if pool.colliderect(self.rect):
                if pool.collidepoint(self.rect.left, self.rect.top):
                    if xoffset < 0:
                        xoffset = 0
                    if yoffset < 0:
                        yoffset = 0
                elif pool.collidepoint(self.rect.left, self.rect.bottom):
                    if xoffset < 0:
                        xoffset = 0
                    if yoffset > 0:
                        yoffset = -0
                elif pool.collidepoint(self.rect.right, self.rect.top):
                    if xoffset > 0:
                        xoffset = -0
                    if yoffset < 0:
                        yoffset = 0
                elif pool.collidepoint(self.rect.right, self.rect.bottom):
                    if xoffset > 0:
                        xoffset = -0
                    if yoffset > 0:
                        yoffset = -0
                elif pool.collidepoint(self.rect.centerx, self.rect.top):
                    if yoffset < 0:
                       yoffset = 0
                elif pool.collidepoint(self.rect.centerx, self.rect.bottom):
                    if yoffset > 0:
                        yoffset = -0
                elif pool.collidepoint(self.rect.left, self.rect.centery):
                    if xoffset < 0:
                        xoffset = 0
                elif pool.collidepoint(self.rect.right, self.rect.centery):
                    if xoffset > 0:
                        xoffset = -0
                else:
                    xoffset = 0
                    yoffset = 0
        # bounce back at border
        if  self.x < 3:
            if xoffset < 0:
                xoffset = 1
        if self.x > 759:
            if xoffset > 0:
                xoffset = -1
        if self.y < 3:
            if yoffset < 0:
                yoffset = 1
        if self.y > 724:
            if yoffset > 0:
                yoffset = -1
        # move & rotate the Tank sprite
        newpos = self.rect.move((xoffset, yoffset))
        self.rect = newpos
